﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using System.IO;

namespace ADayRoiAdmin
{
    public partial class FrmLoadData : Form
    {
        int product_id = -1;
        int menuid = 0;
        AdayroiClass adayroi ;
        SqlConnection con;
        DataTable dt_product = null;
        DataRow findRow = null;
        private BindingManagerBase bm = null;
        private Boolean deleted = false;
        
        public FrmLoadData(int MenuID)
        {
            InitializeComponent();
            this.menuid = MenuID;
            adayroi = new AdayroiClass(ref con);
           
        }

        public void AddControlGrid()
        {
            DataGridViewTextBoxColumn PKID = new DataGridViewTextBoxColumn();
            PKID.DataPropertyName = "PKID";
            PKID.Name = "PKID";
            PKID.Visible = false;

            DataGridViewTextBoxColumn ProductCode = new DataGridViewTextBoxColumn();
            ProductCode.DataPropertyName = "ProductCode";
            ProductCode.Name = "ProductCode";
            ProductCode.HeaderText = "Mã Sản Phẩm";

            DataGridViewTextBoxColumn ProductName = new DataGridViewTextBoxColumn();
            ProductName.DataPropertyName = "Title";
            ProductName.HeaderText = "Tên Sản Phẩm";
            ProductName.Name = "Title";

            DataGridViewTextBoxColumn Price = new DataGridViewTextBoxColumn();
            Price.DataPropertyName = "Price";
            Price.Name = "Giá";

            DataGridViewTextBoxColumn CategoryID = new DataGridViewTextBoxColumn();
            CategoryID.DataPropertyName = "CategoryID";
            CategoryID.Name = "CategoryID";
            CategoryID.Visible = false;
            DataGridViewTextBoxColumn Sort = new DataGridViewTextBoxColumn();
            Sort.DataPropertyName = "Sort";
            Sort.Name = "Sort";
            Sort.HeaderText = "Độ Ưu Tiên";

            DataGridViewCheckBoxColumn IsDisplay = new DataGridViewCheckBoxColumn();
            IsDisplay.DataPropertyName = "IsDisplay";
            IsDisplay.Name = "IsDisplay";
            IsDisplay.HeaderText = "Hiển Thị";

            DataGridViewCheckBoxColumn Parent = new DataGridViewCheckBoxColumn();
            Parent.DataPropertyName = "Parent";
            Parent.Name = "Parent";
            Parent.Visible = false;

            DataGridViewCheckBoxColumn _Description = new DataGridViewCheckBoxColumn();
            _Description.DataPropertyName = "Description";
            _Description.Name = "Description";
            _Description.Visible = false;

            DataGridViewCheckBoxColumn _Username = new DataGridViewCheckBoxColumn();
            _Username.DataPropertyName = "Username";
            _Username.Name = "Username";
            _Username.Visible = false;

            DataGridViewCheckBoxColumn _isVIP = new DataGridViewCheckBoxColumn();
            _isVIP.DataPropertyName = "isVIP";
            _isVIP.Name = "isVIP";
            _isVIP.Visible = false;


            dgv_List.Columns.AddRange(new DataGridViewColumn[] { PKID, ProductCode, ProductName, CategoryID, Sort, IsDisplay, Parent, _Description, _Username, _isVIP });
        }

        private void LoadDataGrid()
        {
            dt_product = adayroi.getList("select c.Parent, p.PKID,p.ProductCode,p.Title,p.CategoryID,p.Sort,p.IsDisplay, p.Description, p.Username, p.isVIP from PersonalSales p join Category c on c.PKID =p.CategoryID where Parent='" + menuid + "'", con);
            dgv_List.DataSource = dt_product;
            bm = dgv_List.BindingContext[dgv_List.DataSource, dgv_List.DataMember];
        }

        private void FrmLoadData_Load(object sender, EventArgs e)
        {
            if (menuid == 4)
            {
                this.Text = "Bán hàng cá nhân";
            }
            else
            {
                this.Text = "Dịch vụ";
                
            }
            AddControlGrid();
            LoadDataGrid();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            frmPersonalSales person = new frmPersonalSales(0,dt_product.NewRow(),menuid);
            person.ShowDialog();
        }       

        private void btn_Xoa_Click(object sender, EventArgs e)
        { 
            DialogResult response = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Xóa dòng?", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button2);
            if (response == DialogResult.Yes)
            {
                deleteRecord();
            }
        }

        private void deleteRecord()
        {
           
                DataTable dt = adayroi.getList("select * from Images where PersonalSalesID=" + product_id, con);
                string sql = "";
                sql = "delete from Prices where PersonalSalesID=" + product_id;
                string folder = "PersonalSales";
                if (menuid == 5)
                    folder = "Products";
                SqlTransaction trans = null;
                con.Open();
                trans = con.BeginTransaction();
                if (adayroi.UpdateSQL(con, ref trans, sql) == 0)
                {
                    sql = "delete from Images where PersonalSalesID=" + product_id;
                    if (adayroi.UpdateSQL(con, ref trans, sql) == 0)
                    {
                        sql = "delete from PersonalSales where PKID = " + product_id;
                        if (adayroi.UpdateSQL(con, ref trans, sql) == 0)
                        {
                            int loi_deleteImageList = 0;
                            #region delete ftp
                            if (dt.Rows.Count > 0)
                            {
                                DataRow findRowImage = dt.Rows[0];
                                if (findRowImage["ImageSmall"].ToString() != "")
                                    loi_deleteImageList = adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/SmallImage/", findRowImage["ImageSmall"].ToString());
                                if (loi_deleteImageList == 0)
                                {
                                    if (findRowImage["ImageLarge"].ToString() != "")
                                        loi_deleteImageList = adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/LargeImage/", findRowImage["ImageLarge"].ToString());
                                    if (loi_deleteImageList == 0)
                                    {
                                        string[] imageList = findRowImage["ImageList"].ToString().Split('@');
                                        foreach (string s in imageList)
                                        {
                                            if (s != "")
                                            {
                                                loi_deleteImageList = adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/ListImage/", s);
                                                if (loi_deleteImageList != 0)
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                            if (loi_deleteImageList == 0)
                            {
                                MessageBox.Show("Thành Công");
                                trans.Commit();
                                con.Close();

                            }
                            else
                            {
                                MessageBox.Show("that bai");
                                trans.Rollback();
                                con.Close();
                            }
                        }

                    }
                    else
                    {
                        MessageBox.Show("that bai");
                        trans.Rollback();
                        con.Close();
                    }

                }
        }
        private void dgv_List_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            product_id = Convert.ToInt32(findRow["PKID"].ToString());
            frmPersonalSales pro = new frmPersonalSales(product_id, findRow,menuid);
            pro.ShowDialog();
            findRow.ItemArray = pro._dr_product.ItemArray;
        
        }

        private void dgv_List_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
             findRow = ((DataRowView)bm.Current).Row;
            product_id = Convert.ToInt32(findRow["PKID"].ToString());
        }

        private void dgv_List_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            DialogResult response = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Xóa dòng?", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button2);
            if (response == DialogResult.Yes)
            {
                deleteRecord();
            }
            else
                e.Cancel = true;
        }
      
    }
}
